package view;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JSeparator;
import javax.swing.JTabbedPane;
import javax.swing.JTree;
import javax.swing.ScrollPaneConstants;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.WindowConstants;
import javax.swing.border.BevelBorder;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreeSelectionModel;

import util.MyButton;
import util.MyTable;
import view.component.base.BaseComponent;
import view.component.base.BasePanel;
import view.component.basecomponent.pojo.ComponentPojo;

import component.auxiliares.Campo;
import component.auxiliares.Progress;
import component.auxiliares.Slider;
import component.auxiliares.Texto;
import component.util.BaseMotionAdapter;

import control.Supervisorio;
import de.javasoft.plaf.synthetica.SyntheticaStandardLookAndFeel;
import entities.Panel;
import entities.Variable;

/**
 * This code was edited or generated using CloudGarden's Jigloo SWT/Swing GUI
 * Builder, which is free for non-commercial use. If Jigloo is being used
 * commercially (ie, by a corporation, company or business for any purpose
 * whatever) then you should purchase a license for each developer using Jigloo.
 * Please visit www.cloudgarden.com for details. Use of Jigloo implies
 * acceptance of these licensing terms. A COMMERCIAL LICENSE HAS NOT BEEN
 * PURCHASED FOR THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED LEGALLY FOR
 * ANY CORPORATE OR COMMERCIAL PURPOSE.
 */
@SuppressWarnings("serial")
public class MainWindow extends javax.swing.JFrame {

	private MyTable tblAcoes;
	private JSeparator jSeparator3;
	private JSeparator jSeparator2;
	private JSeparator jSeparator1;
	private JLabel lblNumVars;
	private JLabel lblAcoes;
	private JLabel lblNumComp;
	private JLabel lblNumPnls;
	private JLabel jLabel1;
	private JScrollPane scrollAcoes;
	private JButton btnLabel;
	private JButton btnAdicionar;
	private JButton btnTubulacao;
	private JButton btnEsteira;
	private JButton btnCaldeira;
	private JButton btnTanque;
	private JButton btnValvula;
	private JButton btnBomba;
	private JButton btnMotor;
	private JPanel pnlInfo;
	private JPanel abaAcoes2;
	private JPanel abaAcoes1;
	private JPanel abaAcoes;
	private JPanel pnlAuxiliares;
	private JPanel pnlOutros;
	private JPanel pnlTransporte;
	private JPanel pnlArmazenamento;
	private JPanel pnlAtuadores;
	private JPanel abaGeral;
	private JPanel abaVariaveis;
	private JPanel pnlAux2;
	private JPanel pnlAux1;
	private JPanel pnlCentro;
	private JPanel pnlLogo;
	private JPanel pnlBody;
	private JPanel pnlMenu;
	private JTree treeComponentes;
	private DefaultMutableTreeNode treeNode;
	private JTabbedPane pnlAbasPnls;
	private JTabbedPane pnlAbas;
	private JTabbedPane pnlComponets;
	private MyButton btnPnl;
	private MyButton btnStop;
	private MyButton btnPlay;
	private MyButton btnAbrir;
	private MyButton btnSalvar;
	private MyButton btnNovo;
	private JMenuBar menuBar;
	private JMenuItem ArquivoSalvar;
	private JMenuItem arquivoAbrir;
	private JMenuItem arquivoNovo;
	private JMenuItem ajudaSobre;
	private JMenuItem ajudaHelp;
	private JMenuItem simulacaoConfigurar;
	private JMenuItem simulacaoIniciar;
	private JMenuItem comunicacaoConfigurar;
	private JMenuItem componenteConfiguracoes;
	private JMenuItem componenteAdicionar;
	private JMenuItem arquivoFechar;
	private JMenuItem comunicacaoTestar;
	private JMenuItem menuAddPanel;
	private JMenuItem menuPanelRename;
	private MyTable tblVariaveis;
	private JScrollPane scrollVar;
	private JButton btnSlider;
	private JButton btnProgress;
	private JButton btnField;
	private JMenuItem menuPanelExclude;
	private JMenuItem menuPanelProperties;
	private JMenu menuComunicacao;
	private JMenu menuAjuda;
	private JMenu menuSimulacao;
	private JMenu menuComponentes;
	private JMenu menuArquivo;
	private JScrollPane scrollAbaGeral;

	private JPopupMenu popupPanel;
	private JPopupMenu popupAba;
	private TableModel tblAcoesModel;
	private TableModel tblVariaveisModel;

	private String icoAdress = "";
	private String selectedComponent = "";

	private Supervisorio supervisorio;
	private ArrayList<BaseComponent> baseComponents;
	private ArrayList<Variable> variables;
	private ArrayList<Object> auxiliares;

	public ArrayList<Variable> getVariables() {
		return variables;
	}

	public void setVariables(ArrayList<Variable> variables) {
		this.variables = variables;
	}

	/**
	 * Auto-generated main method to display this JFrame
	 */
	public static void main(String[] args) {
		try {
			UIManager.setLookAndFeel(new SyntheticaStandardLookAndFeel());
		} catch (Exception e) {
			e.printStackTrace();
		}
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				MainWindow inst = new MainWindow();
				inst.setLocationRelativeTo(null);
				inst.setVisible(true);
				inst.getJMenuBar().setPreferredSize(
						new java.awt.Dimension(792, 27));
			}
		});
	}

	public MainWindow() {
		super();
		// treeModel.addTreeModelListener(this);
		supervisorio = Supervisorio.getInstance();
		supervisorio.setMainWindow(this);

		baseComponents = new ArrayList<BaseComponent>();
		auxiliares = new ArrayList<Object>();
		initGUI();
	}

	private void initGUI() {
		try {
			setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
			this.setTitle("i2M - Sistema Supervisório");
			this.setPreferredSize(new java.awt.Dimension(1024, 768));
			{
				pnlMenu = new JPanel();
				FlowLayout pnlMenuLayout = new FlowLayout();
				pnlMenuLayout.setAlignment(FlowLayout.LEFT);
				pnlMenu.setLayout(pnlMenuLayout);
				getContentPane().add(pnlMenu, BorderLayout.NORTH);
				pnlMenu.setPreferredSize(new java.awt.Dimension(792, 40));
				pnlMenu.setBorder(BorderFactory
						.createEtchedBorder(BevelBorder.LOWERED));
				{
					btnNovo = new MyButton(pnlMenu, "image/icoNovo.png");
					btnNovo.setPreferredSize(new java.awt.Dimension(30, 30));
					btnNovo.addMouseListener(new MouseAdapter() {
						public void mouseClicked(MouseEvent evt) {
							btnNovoMouseExited(evt);
						}
					});
				}
				{
					btnSalvar = new MyButton(pnlMenu, "image/icoSave.png");
					btnSalvar.setPreferredSize(new java.awt.Dimension(30, 30));
				}
				{
					btnAbrir = new MyButton(pnlMenu, "image/openIcon.png");
					btnAbrir.setPreferredSize(new java.awt.Dimension(30, 30));
					btnAbrir.addMouseListener(new MouseAdapter() {
						public void mouseClicked(MouseEvent evt) {
							btnAbrirMouseExited(evt);
						}
					});
				}
				{

					btnPlay = new MyButton(pnlMenu, "image/icoPlay.png");
					btnPlay.setPreferredSize(new java.awt.Dimension(30, 30));
					btnPlay.addMouseListener(new MouseAdapter() {
						public void mouseClicked(MouseEvent evt) {
							simulacaoIniciarActionPerformed();
						}
					});
				}
				{
					btnStop = new MyButton(pnlMenu, "image/icoStop.png");
					btnStop.setPreferredSize(new java.awt.Dimension(30, 30));
					btnStop.addMouseListener(new MouseAdapter() {
						public void mouseClicked(MouseEvent evt) {
							simulacaoPararActionPerformed();
						}
					});
				}
				{
					btnPnl = new MyButton(pnlMenu, "image/icoAdd.png");
					btnPnl.setPreferredSize(new java.awt.Dimension(30, 30));
					btnPnl.setToolTipText("Adicionar painel");
					btnPnl.addMouseListener(new MouseAdapter() {
						public void mouseClicked(MouseEvent evt) {
							btnPnlMouseExited();
						}
					});
				}
			}
			{
				pnlBody = new JPanel();
				BorderLayout pnlBodyLayout = new BorderLayout();
				pnlBody.setLayout(pnlBodyLayout);
				getContentPane().add(pnlBody, BorderLayout.CENTER);
				{
					pnlCentro = new JPanel();
					BorderLayout pnlCentroLayout = new BorderLayout();
					pnlBody.add(pnlCentro, BorderLayout.CENTER);
					pnlCentro.setLayout(pnlCentroLayout);
					pnlCentro.setBackground(new java.awt.Color(224, 224, 224));
					{
						pnlAux1 = new JPanel();
						BorderLayout pnlAux1Layout = new BorderLayout();
						pnlAux1.setLayout(pnlAux1Layout);
						pnlCentro.add(pnlAux1, BorderLayout.CENTER);
						pnlAux1.setBounds(12, 12, 741, 590);
						pnlAux1
								.setBackground(new java.awt.Color(224, 224, 224));
						pnlAux1.setPreferredSize(new java.awt.Dimension(769,
								614));
						{
							pnlComponets = new JTabbedPane();
							pnlAux1.add(pnlComponets, BorderLayout.NORTH);
							pnlComponets
									.setPreferredSize(new java.awt.Dimension(
											761, 68));
							{
								pnlAtuadores = new JPanel();
								FlowLayout pnlAtuadoresLayout = new FlowLayout();
								pnlAtuadoresLayout
										.setAlignment(FlowLayout.LEFT);
								pnlAtuadores.setLayout(pnlAtuadoresLayout);
								pnlComponets.addTab("Atuadores", null,
										pnlAtuadores, null);
								{
									btnMotor = new JButton();
									pnlAtuadores.add(btnMotor);
									btnMotor.setText("Motor");
									btnMotor
											.addActionListener(new ActionListener() {
												public void actionPerformed(
														ActionEvent evt) {
													icoAdress = "image/motor.png";
												}
											});
								}
								{
									btnBomba = new JButton();
									pnlAtuadores.add(btnBomba);
									btnBomba.setText("Bomba");
									btnBomba
											.addActionListener(new ActionListener() {
												public void actionPerformed(
														ActionEvent evt) {
													icoAdress = "image/bomba.png";
												}
											});
								}
								{
									btnValvula = new JButton();
									pnlAtuadores.add(btnValvula);
									btnValvula.setText("Válvula");
									btnValvula
											.addActionListener(new ActionListener() {
												public void actionPerformed(
														ActionEvent evt) {
													icoAdress = "image/valvula.png";
												}
											});
								}
							}
							{
								pnlArmazenamento = new JPanel();
								FlowLayout pnlArmazenamentoLayout = new FlowLayout();
								pnlArmazenamentoLayout
										.setAlignment(FlowLayout.LEFT);
								pnlArmazenamento
										.setLayout(pnlArmazenamentoLayout);
								pnlComponets.addTab("Armazenamento", null,
										pnlArmazenamento, null);
								{
									btnTanque = new JButton();
									pnlArmazenamento.add(btnTanque);
									btnTanque.setText("Tanque");
									btnTanque
											.addActionListener(new ActionListener() {
												public void actionPerformed(
														ActionEvent evt) {
													icoAdress = "image/tanque.png";
												}
											});
								}
								{
									btnCaldeira = new JButton();
									pnlArmazenamento.add(btnCaldeira);
									btnCaldeira.setText("Caldeira");
									btnCaldeira
											.addActionListener(new ActionListener() {
												public void actionPerformed(
														ActionEvent evt) {
													icoAdress = "";
												}
											});
								}
							}
							{
								pnlTransporte = new JPanel();
								FlowLayout pnlTransporteLayout = new FlowLayout();
								pnlTransporteLayout
										.setAlignment(FlowLayout.LEFT);
								pnlTransporte.setLayout(pnlTransporteLayout);
								pnlComponets.addTab("Transporte", null,
										pnlTransporte, null);
								{
									btnEsteira = new JButton();
									pnlTransporte.add(btnEsteira);
									btnEsteira.setText("Esteira");
									btnEsteira
											.addActionListener(new ActionListener() {
												public void actionPerformed(
														ActionEvent evt) {
													icoAdress = "";
												}
											});
								}
								{
									btnTubulacao = new JButton();
									pnlTransporte.add(btnTubulacao);
									btnTubulacao.setText("Tubulação");
									btnTubulacao
											.addActionListener(new ActionListener() {
												public void actionPerformed(
														ActionEvent evt) {
													icoAdress = "image/cano.png";
												}
											});
								}
							}
							{
								pnlOutros = new JPanel();
								pnlComponets.addTab("Outros", null, pnlOutros,
										null);
							}
							{
								pnlAuxiliares = new JPanel();
								FlowLayout pnlAuxiliaresLayout = new FlowLayout();
								pnlAuxiliaresLayout
										.setAlignment(FlowLayout.LEFT);
								pnlAuxiliares.setLayout(pnlAuxiliaresLayout);
								pnlComponets.addTab("Auxiliares", null,
										pnlAuxiliares, null);
								{
									btnLabel = new JButton();
									pnlAuxiliares.add(btnLabel);
									btnLabel.setText("Texto");
									btnLabel
											.setPreferredSize(new java.awt.Dimension(
													68, 21));
									btnLabel
											.addActionListener(new ActionListener() {
												public void actionPerformed(
														ActionEvent evt) {
													selectedComponent = "Label";
												}
											});
								}
								{
									btnField = new JButton();
									pnlAuxiliares.add(btnField);
									btnField.setText("Campo");
									btnField
											.setPreferredSize(new java.awt.Dimension(
													61, 21));
									btnField
											.addActionListener(new ActionListener() {
												public void actionPerformed(
														ActionEvent evt) {

													selectedComponent = "Field";
												}
											});
								}
								{
									btnProgress = new JButton();
									pnlAuxiliares.add(btnProgress);
									btnProgress.setText("Barra gradual");
									btnProgress
											.setPreferredSize(new java.awt.Dimension(
													103, 21));
									btnProgress
											.addActionListener(new ActionListener() {
												public void actionPerformed(
														ActionEvent evt) {
													selectedComponent = "Progresso";
												}
											});
								}
								{
									btnSlider = new JButton();
									pnlAuxiliares.add(btnSlider);
									btnSlider.setText("Slider");
									btnSlider
											.setPreferredSize(new java.awt.Dimension(
													59, 21));
									btnSlider
											.addActionListener(new ActionListener() {
												public void actionPerformed(
														ActionEvent evt) {
													selectedComponent = "Slider";
												}
											});
								}
							}
						}
						{
							pnlAbasPnls = new JTabbedPane();
							pnlAux1.add(pnlAbasPnls, BorderLayout.CENTER);
							pnlAbasPnls.setBackground(new java.awt.Color(128,
									128, 128));
							pnlAbasPnls.addMouseListener(new MouseAdapter() {
								public void mouseClicked(MouseEvent e) {
									pnlAbasPnlsMouseClicked(e);
								}
							});
						}
						{
							pnlInfo = new JPanel();
							FlowLayout pnlInfoLayout = new FlowLayout();
							pnlInfoLayout.setAlignment(FlowLayout.LEFT);
							pnlAux1.add(pnlInfo, BorderLayout.SOUTH);
							pnlInfo.setLayout(pnlInfoLayout);
							pnlInfo.setPreferredSize(new java.awt.Dimension(
									761, 29));
							{
								lblNumPnls = new JLabel();
								pnlInfo.add(lblNumPnls);
								lblNumPnls.setText("Paineis: 0");
								lblNumPnls.setEnabled(false);
							}
							{
								jSeparator2 = new JSeparator();
								pnlInfo.add(jSeparator2);
								jSeparator2
										.setOrientation(SwingConstants.VERTICAL);
								jSeparator2
										.setPreferredSize(new java.awt.Dimension(
												2, 19));
							}
							{
								lblNumComp = new JLabel();
								pnlInfo.add(lblNumComp);
								lblNumComp.setText("Componentes: 18");
								lblNumComp.setEnabled(false);
							}
							{
								jSeparator1 = new JSeparator();
								pnlInfo.add(jSeparator1);
								jSeparator1
										.setPreferredSize(new java.awt.Dimension(
												5, 19));
								jSeparator1
										.setOrientation(SwingConstants.VERTICAL);
							}
							{
								lblNumVars = new JLabel();
								pnlInfo.add(lblNumVars);
								lblNumVars.setText("Variáveis: 22");
								lblNumVars.setEnabled(false);
							}
							{
								jSeparator3 = new JSeparator();
								pnlInfo.add(jSeparator3);
								jSeparator3
										.setOrientation(SwingConstants.VERTICAL);
								jSeparator3
										.setPreferredSize(new java.awt.Dimension(
												4, 19));
							}
							{
								jLabel1 = new JLabel();
								pnlInfo.add(jLabel1);
								jLabel1.setText("Ações: 13");
								jLabel1.setEnabled(false);
							}
						}
					}
					{
						pnlAux2 = new JPanel();
						BorderLayout pnlAux2Layout = new BorderLayout();
						pnlAux2.setLayout(pnlAux2Layout);
						pnlCentro.add(pnlAux2, BorderLayout.EAST);
						pnlAux2.setBounds(765, 12, 236, 590);
						pnlAux2.setBorder(BorderFactory
								.createEtchedBorder(BevelBorder.LOWERED));
						pnlAux2.setPreferredSize(new java.awt.Dimension(255,
								614));
						{
							pnlAbas = new JTabbedPane();
							pnlAbas.addMouseListener(new MouseAdapter() {
								public void mouseClicked(MouseEvent evt) {
									abaVarActionPerformed();
								}
							});
							pnlAux2.add(pnlAbas, BorderLayout.CENTER);
							{
								abaGeral = new JPanel();
								pnlAbas
										.addTab("Explorer", null, abaGeral,
												null);
								BorderLayout abaGeralLayout = new BorderLayout();
								abaGeral.setLayout(abaGeralLayout);
								{
									// TODO
									treeNode = new DefaultMutableTreeNode(
											"Geral");
									treeComponentes = new JTree(treeNode);
									treeComponentes
											.getSelectionModel()
											.setSelectionMode(
													TreeSelectionModel.SINGLE_TREE_SELECTION);
									treeComponentes.setShowsRootHandles(true);

									abaGeral.add(treeComponentes,
											BorderLayout.CENTER);
								}
							}
							{
								abaVariaveis = new JPanel();
								BorderLayout abaVariaveisLayout = new BorderLayout();
								abaVariaveis.setLayout(abaVariaveisLayout);
								{
									scrollVar = new JScrollPane();
									abaVariaveis.add(scrollVar,
											BorderLayout.CENTER);
									scrollVar
											.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
									scrollVar
											.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
									{
										tblVariaveisModel = new DefaultTableModel(
												new String[][] {},
												new String[] { "Nome", "Valor" });
										tblVariaveis = new MyTable();
										scrollVar.setViewportView(tblVariaveis);
										tblVariaveis
												.setModel(tblVariaveisModel);
									}
								}
							}
							{
								abaAcoes = new JPanel();
								BorderLayout abaAcoesLayout = new BorderLayout();
								abaAcoes.setLayout(abaAcoesLayout);
								pnlAbas.addTab("Ações", null, abaAcoes, null);
								{
									abaAcoes1 = new JPanel();
									BorderLayout abaAcoes1Layout = new BorderLayout();
									abaAcoes1.setLayout(abaAcoes1Layout);
									abaAcoes
											.add(abaAcoes1, BorderLayout.CENTER);
									{
										scrollAcoes = new JScrollPane();
										abaAcoes1.add(scrollAcoes,
												BorderLayout.CENTER);
										scrollAcoes
												.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
										scrollAcoes
												.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
										{
											tblAcoes = new MyTable();
											scrollAcoes
													.setViewportView(tblAcoes);
											tblAcoesModel = new DefaultTableModel(
													new String[][] {},
													new String[] { "Ação",
															"Variável",
															"Condição" });
											tblAcoes.setModel(tblAcoesModel);
										}
									}
								}
								{
									abaAcoes2 = new JPanel();
									abaAcoes.add(abaAcoes2, BorderLayout.NORTH);
									abaAcoes2
											.setPreferredSize(new java.awt.Dimension(
													246, 31));
									abaAcoes2.setLayout(null);
									{
										lblAcoes = new JLabel();
										abaAcoes2.add(lblAcoes);
										lblAcoes.setText("Ações");
										lblAcoes.setBounds(7, 6, 43, 18);
										lblAcoes.setFont(new java.awt.Font(
												"Tahoma", 1, 11));
									}
									{
										btnAdicionar = new JButton();
										abaAcoes2.add(btnAdicionar);
										btnAdicionar.setText("Adicionar...");
										btnAdicionar.setBounds(99, 5, 136, 21);
										btnAdicionar
												.addActionListener(new ActionListener() {
													public void actionPerformed(
															ActionEvent evt) {
														btnAdicionarActionPerformed(evt);
													}
												});
									}
								}
							}
						}
					}
				}
				{
					pnlLogo = new JPanel();
					FlowLayout pnlLogoLayout = new FlowLayout();
					pnlLogoLayout.setAlignment(FlowLayout.RIGHT);
					pnlLogo.setLayout(pnlLogoLayout);
					pnlBody.add(pnlLogo, BorderLayout.SOUTH);
					pnlLogo.setPreferredSize(new java.awt.Dimension(1016, 58));
					pnlLogo.setBackground(new java.awt.Color(192, 192, 192));
					pnlLogo.setBorder(BorderFactory
							.createEtchedBorder(BevelBorder.LOWERED));
				}
			}
			{
				menuBar = new JMenuBar();
				setJMenuBar(menuBar);
				{
					menuArquivo = new JMenu();
					menuBar.add(menuArquivo);
					menuArquivo.setText("Arquivo");
					{
						arquivoNovo = new JMenuItem();
						menuArquivo.add(arquivoNovo);
						arquivoNovo.setText("Nova planta...");
					}
					{
						arquivoAbrir = new JMenuItem();
						menuArquivo.add(arquivoAbrir);
						arquivoAbrir.setText("Abrir...");
					}
					{
						ArquivoSalvar = new JMenuItem();
						menuArquivo.add(ArquivoSalvar);
						ArquivoSalvar.setText("Salvar...");
					}
					{
						arquivoFechar = new JMenuItem();
						menuArquivo.add(arquivoFechar);
						arquivoFechar.setText("Fechar...");
					}
				}
				{
					menuComponentes = new JMenu();
					menuBar.add(menuComponentes);
					menuComponentes.setText("Componentes");
					{
						componenteAdicionar = new JMenuItem();
						menuComponentes.add(componenteAdicionar);
						componenteAdicionar.setText("Adicionar...");
						componenteAdicionar
								.addActionListener(new ActionListener() {
									public void actionPerformed(ActionEvent evt) {
										componenteAdicionarActionPerformed(evt);
									}
								});
					}
					{
						componenteConfiguracoes = new JMenuItem();
						menuComponentes.add(componenteConfiguracoes);
						componenteConfiguracoes.setText("Configurações...");
						componenteConfiguracoes
								.addActionListener(new ActionListener() {
									public void actionPerformed(ActionEvent evt) {
										componenteConfiguracoesActionPerformed(evt);
									}
								});
					}
				}
				{
					menuComunicacao = new JMenu();
					menuBar.add(menuComunicacao);
					menuComunicacao.setText("Comunicação");
					{
						comunicacaoTestar = new JMenuItem();
						menuComunicacao.add(comunicacaoTestar);
						comunicacaoTestar.setText("Testar...");
						comunicacaoTestar
								.addActionListener(new ActionListener() {
									public void actionPerformed(ActionEvent evt) {
										comunicacaoTestarActionPerformed(evt);
									}
								});
					}
					{
						comunicacaoConfigurar = new JMenuItem();
						menuComunicacao.add(comunicacaoConfigurar);
						comunicacaoConfigurar.setText("Configurar...");
						comunicacaoConfigurar
								.addActionListener(new ActionListener() {
									public void actionPerformed(ActionEvent evt) {
										comunicacaoConfigurarActionPerformed(evt);
									}
								});
					}
				}
				{
					menuSimulacao = new JMenu();
					menuBar.add(menuSimulacao);
					menuSimulacao.setText("Simulação");
					{
						simulacaoIniciar = new JMenuItem();
						menuSimulacao.add(simulacaoIniciar);
						simulacaoIniciar.setText("Iniciar...");
						simulacaoIniciar
								.addActionListener(new ActionListener() {
									public void actionPerformed(ActionEvent evt) {
										simulacaoIniciarActionPerformed();
									}
								});
					}
					{
						simulacaoConfigurar = new JMenuItem();
						menuSimulacao.add(simulacaoConfigurar);
						simulacaoConfigurar.setText("Configurar...");
						simulacaoConfigurar
								.addActionListener(new ActionListener() {
									public void actionPerformed(ActionEvent evt) {
										simulacaoConfigurarActionPerformed(evt);
									}
								});
					}
				}
				{
					menuAjuda = new JMenu();
					menuBar.add(menuAjuda);
					menuAjuda.setText("Ajuda");
					{
						ajudaHelp = new JMenuItem();
						menuAjuda.add(ajudaHelp);
						ajudaHelp.setText("Help...");
					}
					{
						ajudaSobre = new JMenuItem();
						menuAjuda.add(ajudaSobre);
						ajudaSobre.setText("Sobre...");
					}
				}
				{
					popupPanel = new JPopupMenu();
					{
						menuPanelRename = new JMenuItem("Renomear...");
						menuPanelRename.addActionListener(new ActionListener() {
							public void actionPerformed(ActionEvent evt) {
								menuPanelRenameActionPerformed(evt);
							}
						});
						popupPanel.add(menuPanelRename);
					}
					{
						menuPanelExclude = new JMenuItem("Excluir");
						menuPanelExclude
								.addActionListener(new ActionListener() {
									public void actionPerformed(ActionEvent evt) {
										menuPanelExcludeActionPerformed(evt);
									}
								});
						popupPanel.add(menuPanelExclude);
					}
					{
						menuPanelProperties = new JMenuItem("Propriedades...");
						menuPanelProperties
								.addActionListener(new ActionListener() {
									public void actionPerformed(ActionEvent evt) {
										menuPanelPropertiesActionPerformed(evt);
									}
								});
						popupPanel.add(menuPanelProperties);
					}
				}
				{
					popupAba = new JPopupMenu();
					{
						menuAddPanel = new JMenuItem("Adicionar painel...");
						popupAba.add(menuAddPanel);
						menuAddPanel.addActionListener(new ActionListener() {
							public void actionPerformed(ActionEvent evt) {
								menuAddPanelActionPerformed(evt);
							}
						});
					}
				}
			}
			this.addWindowListener(new WindowAdapter() {
				public void windowClosed(WindowEvent e) {
					supervisorio.doOnClose();
				}
			});

			this.disableAll();
			pack();
			this.setSize(1024, 768);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void disableAll() {
		btnPnl.disableListeners();
		btnStop.disableListeners();
		btnPlay.disableListeners();
		btnSalvar.disableListeners();
		btnAdicionar.setEnabled(false);
	}

	public void enableAll() {
		btnPnl.enableListeners();
		btnStop.enableListeners();
		btnPlay.enableListeners();
		btnSalvar.enableListeners();
		btnAdicionar.setEnabled(true);
		this.buildTree();
	}

	protected static ImageIcon createImageIcon(String path) {
		java.net.URL imgURL = MainWindow.class.getResource(path);
		if (imgURL != null) {
			return new ImageIcon(imgURL);
		} else {
			System.err.println("Couldn't find file: " + path);
			return null;
		}
	}

	private void panelMouseClicked(MouseEvent evt) {

		BasePanel basePanel = (BasePanel) evt.getComponent();

		if (evt.getButton() == MouseEvent.BUTTON3) {
			popupPanel.show(basePanel, evt.getX(), evt.getY());
		} else if (selectedComponent.equals("Botao")) {
			String p = JOptionPane.showInputDialog("Texto:");
			JButton button = new JButton(p);
			button.setSize(60, 20);
			basePanel.add(button);
			button.setLocation(evt.getX(), evt.getY());
			selectedComponent = "";
			new BaseMotionAdapter(button, basePanel);
			button.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent evt) {
					buttonActionPerformed(evt);
				}
			});
		} else if (selectedComponent.equals("Label")) {
			String x = JOptionPane.showInputDialog("Texto:");
			Texto texto = new Texto(basePanel, x, evt);
			selectedComponent = "";

		} else if (selectedComponent.equals("Field")) {
			Campo campo = new Campo(basePanel, evt);
			selectedComponent = "";

			this.auxiliares.add(campo);

		} else if (selectedComponent.equals("Progresso")) {
			Progress progress = new Progress(basePanel, evt);
			selectedComponent = "";

			this.auxiliares.add(progress);

		} else if (selectedComponent.equals("Slider")) {
			Slider slider = new Slider(basePanel, evt);
			selectedComponent = "";

			this.auxiliares.add(slider);

		} else if (!icoAdress.equals("")) {

			BaseComponent baseComponent = new BaseComponent(basePanel,
					icoAdress, evt);

			String name = selectedComponent + "["
					+ basePanel.getComponentCount() + "]";

			ComponentPojo compPojo = supervisorio.createComponent(name,
					baseComponent.getX(), baseComponent.getY(), basePanel
							.getPanel());
			baseComponent.setPojo(compPojo);

			this.baseComponents.add(baseComponent);

			this.atualizaDados();
			selectedComponent = "";
			icoAdress = "";

		}
	}

	private void buttonActionPerformed(ActionEvent evt) {
		// JOptionPane.showMessageDialog(null, "Clicou sobre botao");
	}

	private void pnlAbasPnlsMouseClicked(MouseEvent e) {
		if (e.getButton() == MouseEvent.BUTTON3) {
			popupAba.show(e.getComponent(), e.getX(), e.getY());
		}
	}

	public void atualizaDados() {
		lblNumPnls.setText("Paineis:" + pnlAbasPnls.getComponentCount());
		lblNumComp.setText("Componentes: " + baseComponents.size());
		this.buildTree();
	}

	private void btnPnlMouseExited() {
		String nome = JOptionPane
				.showInputDialog("Insira o nome do novo painel");
		try {
			if (!nome.trim().equals("")) {

				JScrollPane scroll = new JScrollPane();

				BasePanel newPanel = new BasePanel();
				scroll.setViewportView(newPanel);
				scroll
						.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
				scroll
						.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
				scroll.setBackground(new Color(255, 255, 255));

				newPanel.setBackground(new Color(255, 255, 255));
				newPanel.setPreferredSize(new Dimension(1008, 714));
				newPanel.setLayout(null);
				newPanel.setName(nome);

				newPanel.addMouseListener(new MouseAdapter() {
					public void mouseClicked(MouseEvent evt) {
						panelMouseClicked(evt);
					}
				});

				pnlAbasPnls.addTab(nome, scroll);

				Panel pn = supervisorio.createPanel(nome);

				if (pn != null) {
					newPanel.setPanel(pn);
				} else
					JOptionPane.showMessageDialog(null, "nao deu");

				this.atualizaDados();
			} else {
				JOptionPane.showMessageDialog(null,
						"Não é possível inserir uma nova aba sem nome",
						"Erro!", 2);
			}
		} catch (NullPointerException e) {
			e.printStackTrace();
		}
	}

	private void btnAbrirMouseExited(MouseEvent evt) {
		// TODO
	}

	private void btnAdicionarActionPerformed(ActionEvent evt) {
		if (evt.getSource() == btnAdicionar) {
			ActionWindow w = new ActionWindow();
			w.setVisible(true);
		}
	}

	private void comunicacaoConfigurarActionPerformed(ActionEvent evt) {
		ConfigWindow configWindow = new ConfigWindow();
		configWindow.setVisible(true);
	}

	private void comunicacaoTestarActionPerformed(ActionEvent evt) {
		TestWindow tw = new TestWindow();
		tw.setVisible(true);
	}

	private void menuAddPanelActionPerformed(ActionEvent evt) {
		this.btnPnlMouseExited();
	}

	private void menuPanelPropertiesActionPerformed(ActionEvent evt) {
		// TODO Auto-generated method stub
	}

	private void menuPanelExcludeActionPerformed(ActionEvent evt) {
		// TODO Auto-generated method stub
	}

	private void menuPanelRenameActionPerformed(ActionEvent evt) {
		// TODO Auto-generated method stub
	}

	private void simulacaoConfigurarActionPerformed(ActionEvent evt) {
		// TODO Auto-generated method stub
	}

	private void componenteAdicionarActionPerformed(ActionEvent evt) {
		// TODO Auto-generated method stub
	}

	private void abaVarActionPerformed() {
		if (pnlAbas.getSelectedIndex() == 2) {
			supervisorio.setVariables(supervisorio.getAllVariables());
		}
	}

	private void simulacaoIniciarActionPerformed() {
		pnlAbas.addTab("Variaveis", null, abaVariaveis, null);
		pnlAbas.setSelectedIndex(2);
		supervisorio.initiateCommunication();
		for (BaseComponent currentComponent : baseComponents) {
			currentComponent.disableListeners();
		}
		for (int i = 0; i < auxiliares.size(); i++) {
			try {
				Campo campo = (Campo) auxiliares.get(i);
				campo.disableListeners();
			} catch (Exception e) {
				try {
					Texto texto = (Texto) auxiliares.get(i);
					texto.disableListeners();
				} catch (Exception e2) {
					Progress progress = (Progress) auxiliares.get(i);
					progress.disableListeners();
				}
			}
		}
	}

	private void simulacaoPararActionPerformed() {
		pnlAbas.remove(abaVariaveis);
		pnlAbas.setSelectedIndex(0);
		supervisorio.stopCommunication();
		for (BaseComponent currentComponent : baseComponents) {
			currentComponent.enableListeners();
		}
		for (int i = 0; i < auxiliares.size(); i++) {
			try {
				Campo campo = (Campo) auxiliares.get(i);
				campo.enableListeners();
			} catch (Exception e) {
				try {
					Texto texto = (Texto) auxiliares.get(i);
					texto.enableListeners();
				} catch (Exception e2) {
					Progress progress = (Progress) auxiliares.get(i);
					progress.enableListeners();
				}
			}
		}
	}

	private void componenteConfiguracoesActionPerformed(ActionEvent evt) {
		// TODO Auto-generated method stub
	}

	private void btnNovoMouseExited(MouseEvent evt) {
		NewProjectWindow inst = new NewProjectWindow();
		inst.setLocationRelativeTo(null);
		inst.setVisible(true);
	}

	public void updateVariableTbl(String[][] rows) {
		try {
			// tblVariaveisModel.getValueAt(0, 0);

			for (int i = 0; i < rows.length; i++) {
				tblVariaveis.setValueAt(rows[i][0], i, 0);
				tblVariaveis.setValueAt(rows[i][1], i, 1);
			}

		} catch (Exception e) {
			tblVariaveisModel = new DefaultTableModel(rows, new String[] {
					"Nome", "Valor" });
			tblVariaveis.setModel(tblVariaveisModel);
		}

	}

	public void setTblVariaveis(MyTable tblVariaveis) {
		this.tblVariaveis = tblVariaveis;
	}

	public MyTable getTblVariaveis() {
		return tblVariaveis;
	}

	public ArrayList<Object> getAuxiliares() {
		return auxiliares;
	}

	public void setAuxiliares(ArrayList<Object> auxiliares) {
		this.auxiliares = auxiliares;
	}

	public void buildTree() {
		treeNode = supervisorio.buildTree();
		abaGeral.removeAll();
		treeComponentes = new JTree(treeNode);
		abaGeral.add(treeComponentes, BorderLayout.CENTER);
		treeComponentes.repaint();
		pnlAbas.repaint();
	}
}
